package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.LinkDaoIn;
import com.aerotechnic.entities.Link;

@Repository("LinkDao")
public class LinkDao implements LinkDaoIn {
	
	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(Link myEntity) {
		// TODO Auto-generated method stub
		getCurrentSession().save(myEntity);
	}

	@Override
	public void update(Link link) {
		// TODO Auto-generated method stub
		getCurrentSession().save(link);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Link e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);
	}

	@Override
	public Link findByID(Long id) {
		// TODO Auto-generated method stub
		
		return (Link) getCurrentSession().createQuery("FROM Link where id= " + id)
				.list().get(0);
	}

	@Override
	public List<Link> findAll() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Link").list();
	}

}
